package Server

import (
	"context"
	"fmt"
	"strings"
	"time"

	"github.com/gogf/gf/v2/util/gconv"
	"github.com/wzh200x/hyperNexus/hyperNexusModel"
	"github.com/wzh200x/hyperNexus/hyperNexusProtocol/commonProtocol"
	"github.com/wzh200x/hyperNexus/hyperNexusType"
	emptypb "google.golang.org/protobuf/types/known/emptypb"
)

// DBOperateImp servant implementation
type DBOperateImp struct {
}

func (imp *DBOperateImp) QueryAccount(ctx context.Context,
	input commonProtocol.AccountQuery) (output commonProtocol.Account, err error) {
	// implement your code here:
	e, err := QueryModel[hyperNexusModel.Account](hyperNexusType.Account,
		input.AccountName,
		"accountName =?",
		[]string{input.AccountName})
	if err != nil {
		return output, err
	}
	FormatAccountResponse(e, &output)
	return output, nil
}

func (imp *DBOperateImp) CreateAccount(ctx context.Context, input commonProtocol.Account) (output commonProtocol.Account, err error) {
	// implement your code here:

	e, err := QueryModel[hyperNexusModel.Account](hyperNexusType.Account,
		input.AccountName,
		"accountName =?",
		[]string{input.AccountName})
	if err == nil {
		return output, fmt.Errorf("account already exists")
	}
	e, err = CreateModel[hyperNexusModel.Account](hyperNexusType.Account, input.AccountName)
	if err != nil {
		return output, err
	}
	e.AccountName = input.AccountName
	e.PassWord = input.PassWord
	e.ThirdPlatformType = gconv.Int32(input.ThirdPlatformType)
	e.ThirdPlatform = input.ThirdPlatform
	e.ThirdAccount = input.ThirdAccount
	e.ThirdToken = input.ThirdToken
	e.ThirdRefreshToken = input.ThirdRefreshToken
	e.ThirdTokenExpireTime = input.ThirdTokenExpireTime
	e.ThirdSessionKey = input.ThirdSessionKey
	err = UpdateModel(hyperNexusType.Account, e.AccountName, e)
	FormatAccountResponse(e, &output)
	return output, nil
}

func (imp *DBOperateImp) UpdateAccount(ctx context.Context,
	input commonProtocol.Account) (output commonProtocol.Account, err error) {
	// implement your code here:
	if input.ID == 0 {
		return output, fmt.Errorf("id is required")
	}
	e, err := QueryModel[hyperNexusModel.Account](hyperNexusType.Account,
		input.AccountName,
		"accountName =?",
		[]string{input.AccountName})
	if err != nil {
		return output, err
	}
	e.PassWord = input.PassWord
	e.AvatarId = input.AvatarId
	e.HyperNexusAvatarId = input.HyperNexusAvatarId
	e.ThirdPlatformType = gconv.Int32(input.ThirdPlatformType)
	e.ThirdPlatform = input.ThirdPlatform
	e.ThirdAccount = input.ThirdAccount
	e.ThirdToken = input.ThirdToken
	e.ThirdRefreshToken = input.ThirdRefreshToken
	e.ThirdTokenExpireTime = input.ThirdTokenExpireTime
	e.ThirdSessionKey = input.ThirdSessionKey
	e.ThirdActiveReport = input.ThirdActiveReport
	err = UpdateModel(hyperNexusType.Account, e.AccountName, e)
	if err != nil {
		return output, err
	}
	FormatAccountResponse(e, &output)
	return output, nil
}

func (imp *DBOperateImp) CreatAvatar(ctx context.Context,
	input commonProtocol.AvatarCreate) (output commonProtocol.Avatar, err error) {
	// implement your code here:
	// 查询账号信息
	eAccount, err := QueryModel[hyperNexusModel.Account](hyperNexusType.Account,
		input.AccountName,
		"accountName =?",
		[]string{input.AccountName})
	if err != nil {
		return output, err
	}
	if eAccount == nil {
		return output, fmt.Errorf("account not found")
	}
	e, err := CreateModel[hyperNexusModel.Avatar](hyperNexusType.Avatar, input.AccountName)
	if err != nil {
		return output, err
	}
	shareNum := GetModelShareNum(hyperNexusType.Avatar, eAccount.AccountName)
	e.HyperNexusAvatarId = GDBApp.GenerateID(shareNum)
	e.Data = input.Data
	err = UpdateModel(hyperNexusType.Avatar,
		eAccount.AccountName, e)
	if err != nil {
		return output, err
	}
	FormatAvatarResponse(e, &output)
	return output, nil
}

func (imp *DBOperateImp) QueryAvatar(ctx context.Context,
	input commonProtocol.AvatarQuery) (output commonProtocol.Avatar, err error) {
	queryKey := fmt.Sprintf("id = ?")
	queryValue := []string{fmt.Sprintf("%d", input.AvatarId)}
	if input.HyperNexusAvatarId > 0 {
		queryKey = fmt.Sprintf("hyperNexusAvatarId = ?")
		queryValue = []string{fmt.Sprintf("%d", input.HyperNexusAvatarId)}
	}
	e, err := QueryModel[hyperNexusModel.Avatar](hyperNexusType.Avatar,
		input.AccountName, queryKey, queryValue)
	if err != nil {
		return output, err
	}
	FormatAvatarResponse(e, &output)
	return output, nil
}

func (imp *DBOperateImp) UpdateAvatar(ctx context.Context,
	input commonProtocol.AvatarUpdate) (output commonProtocol.Avatar, err error) {
	if input.Avatar.ID <= 0 {
		return output, fmt.Errorf("id is required")
	}
	e, err := QueryModel[hyperNexusModel.Avatar](hyperNexusType.Avatar,
		input.AccountName, "id =?", []string{fmt.Sprintf("%d", input.Avatar.ID)})
	e.Data = input.Avatar.Data
	e.UseServer = gconv.Uint8(input.Avatar.UseServer)
	err = UpdateModel(hyperNexusType.Avatar, input.AccountName, e)
	if err != nil {
		return output, err
	}
	FormatAvatarResponse(e, &output)
	return output, nil
}

func (imp *DBOperateImp) CreatAvatarModule(ctx context.Context, input commonProtocol.AvatarModuleCreate) (output commonProtocol.AvatarModule, err error) {
	// implement your code here:
	// 查询角色信息
	queryKey := "id = ?"
	queryValue := []string{fmt.Sprintf("%d", input.AvatarId)}
	if input.HyperNexusAvatarId > 0 {
		queryKey = "hyperNexusAvatarId = ?"
		queryValue = []string{fmt.Sprintf("%d", input.HyperNexusAvatarId)}
	}

	_, err = QueryModel[hyperNexusModel.Avatar](hyperNexusType.Avatar,
		input.AccountName, queryKey, queryValue)
	if err != nil {
		return output, fmt.Errorf("avatar not found")
	}

	// 检查是否已存在相同模块
	queryModuleKey := "hyperNexusAvatarId = ? AND moduleId = ?"
	var args []interface{}
	args = append(args, input.HyperNexusAvatarId, input.ModuleId)

	_, err = QueryModel[hyperNexusModel.AvatarModule](hyperNexusType.AvatarModule,
		input.AccountName, queryModuleKey, args...)
	if err == nil {
		return output, fmt.Errorf("avatar module already exists")
	}

	// 创建新模块
	e, err := CreateModel[hyperNexusModel.AvatarModule](hyperNexusType.AvatarModule, input.AccountName)
	if err != nil {
		return output, err
	}

	// 设置模块数据
	e.AvatarId = input.AvatarId
	e.HyperNexusAvatarId = input.HyperNexusAvatarId
	e.ModuleId = input.ModuleId
	e.Data = input.Data
	e.IsCompress = false // 默认不压缩

	// 更新到数据库
	err = UpdateModel(hyperNexusType.AvatarModule, input.AccountName, e)
	if err != nil {
		return output, err
	}

	// 格式化响应
	output.ID = e.ID
	output.CreateTime = e.CreatedAt.Format("2006-01-02 15:04:05")
	output.UpdateTime = e.UpdatedAt.Format("2006-01-02 15:04:05")
	output.AvatarId = e.AvatarId
	output.HyperNexusAvatarId = e.HyperNexusAvatarId
	output.ModuleId = e.ModuleId
	output.Data = e.Data
	output.IsCompress = e.IsCompress

	return output, nil
}

func (imp *DBOperateImp) QueryAvatarModule(ctx context.Context, input commonProtocol.AvatarModuleQuery) (output commonProtocol.AvatarModule, err error) {
	// implement your code here:
	// 构建查询条件
	var queryKey string
	var args []interface{}
	// 根据查询条件优先级：1. HyperNexusAvatarId和ModuleId 2. AvatarId和ModuleId
	if input.HyperNexusAvatarId > 0 && input.ModuleId > 0 {
		queryKey = "hyperNexusAvatarId = ? AND moduleId = ?"
		args = append(args, input.HyperNexusAvatarId, input.ModuleId)
	} else if input.AvatarId > 0 && input.ModuleId > 0 {
		queryKey = "avatarId = ? AND moduleId = ?"
		args = append(args, input.AvatarId, input.ModuleId)
	} else {
		return output, fmt.Errorf("invalid query parameters")
	}

	// 查询数据库
	e, err := QueryModel[hyperNexusModel.AvatarModule](hyperNexusType.AvatarModule,
		input.AccountName, queryKey, args...)
	if err != nil {
		return output, err
	}

	// 格式化响应
	output.ID = e.ID
	output.CreateTime = e.CreatedAt.Format("2006-01-02 15:04:05")
	output.UpdateTime = e.UpdatedAt.Format("2006-01-02 15:04:05")
	output.AvatarId = e.AvatarId
	output.HyperNexusAvatarId = e.HyperNexusAvatarId
	output.ModuleId = e.ModuleId
	output.Data = e.Data
	output.IsCompress = e.IsCompress

	return output, nil
}

func (imp *DBOperateImp) UpdateAvatarModule(ctx context.Context, input commonProtocol.AvatarModuleUpdate) (output commonProtocol.AvatarModule, err error) {
	// implement your code here:
	// 构建查询条件
	var queryKey string
	var args []interface{}
	// 根据查询条件优先级：1. HyperNexusAvatarId和ModuleId 2. AvatarId和ModuleId
	if input.HyperNexusAvatarId > 0 && input.ModuleId > 0 {
		queryKey = "hyperNexusAvatarId = ? AND moduleId = ?"
		args = append(args, input.HyperNexusAvatarId, input.ModuleId)
	} else if input.AvatarId > 0 && input.ModuleId > 0 {
		queryKey = "avatarId = ? AND moduleId = ?"
		args = append(args, input.AvatarId, input.ModuleId)
	} else {
		return output, fmt.Errorf("invalid update parameters")
	}

	// 查询数据库
	e, err := QueryModel[hyperNexusModel.AvatarModule](hyperNexusType.AvatarModule,
		input.AccountName, queryKey, args...)
	if err != nil {
		return output, err
	}

	// 更新模块数据
	e.Data = input.Data

	// 更新到数据库
	err = UpdateModel(hyperNexusType.AvatarModule, input.AccountName, e)
	if err != nil {
		return output, err
	}

	// 格式化响应
	output.ID = e.ID
	output.CreateTime = e.CreatedAt.Format("2006-01-02 15:04:05")
	output.UpdateTime = e.UpdatedAt.Format("2006-01-02 15:04:05")
	output.AvatarId = e.AvatarId
	output.HyperNexusAvatarId = e.HyperNexusAvatarId
	output.ModuleId = e.ModuleId
	output.Data = e.Data
	output.IsCompress = e.IsCompress

	return output, nil
}

func (imp *DBOperateImp) QueryCdKey(ctx context.Context, input commonProtocol.CdKeyQuery) (output commonProtocol.CdKey, err error) {
	// implement your code here:
	queryKey := "id = ?"
	queryValue := []string{input.Code}
	if input.Code != "" {
		queryKey = "code = ?"
		queryValue = []string{input.Code}
	}
	e, err := QueryModel[hyperNexusModel.CDKey](hyperNexusType.CDKey,
		"", queryKey, queryValue)
	if err != nil {
		return output, err
	}

	// Format response
	output.ID = e.ID
	output.Code = e.Code
	output.GroupId = e.GroupId
	output.CanUseCount = e.CanUseCount
	output.UsedCount = e.UsedCount
	output.LimitTime = e.LimitTime.Format("2006-01-02 15:04:05")
	return output, nil
}

func (imp *DBOperateImp) CreateCdKey(ctx context.Context, input commonProtocol.CdKey) (output commonProtocol.CdKey, err error) {
	// implement your code here:
	e, err := QueryModel[hyperNexusModel.CDKey](hyperNexusType.CDKey,
		"", "code = ?", []string{input.Code})
	if err == nil {
		return output, fmt.Errorf("cdkey already exists")
	}

	e, err = CreateModel[hyperNexusModel.CDKey](hyperNexusType.CDKey, "")
	if err != nil {
		return output, err
	}

	e.Code = input.Code
	e.GroupId = input.GroupId
	// 将字符串时间转换为time.Time类型
	e.LimitTime = gconv.Time(input.LimitTime)
	e.CanUseCount = input.CanUseCount
	e.UsedCount = input.UsedCount

	err = UpdateModel(hyperNexusType.CDKey, "", e)
	if err != nil {
		return output, err
	}

	// Format response
	output.ID = e.ID
	output.Code = e.Code
	output.GroupId = e.GroupId
	output.CanUseCount = e.CanUseCount
	output.UsedCount = e.UsedCount
	output.LimitTime = e.LimitTime.Format("2006-01-02 15:04:05")
	return output, nil
}

func (imp *DBOperateImp) UpdateCdKey(ctx context.Context, input commonProtocol.CdKey) (output commonProtocol.CdKey, err error) {
	// implement your code here:
	if input.ID == 0 {
		return output, fmt.Errorf("id is required")
	}

	e, err := QueryModel[hyperNexusModel.CDKey](hyperNexusType.CDKey,
		"", "id = ?", []string{fmt.Sprintf("%d", input.ID)})
	if err != nil {
		return output, err
	}

	e.Code = input.Code
	e.GroupId = input.GroupId
	e.CanUseCount = input.CanUseCount
	e.UsedCount = input.UsedCount
	e.LimitTime = gconv.Time(input.LimitTime)

	err = UpdateModel(hyperNexusType.CDKey, "", e)
	if err != nil {
		return output, err
	}

	// Format response
	output.ID = e.ID
	output.Code = e.Code
	output.GroupId = e.GroupId
	output.CanUseCount = e.CanUseCount
	output.UsedCount = e.UsedCount
	output.LimitTime = e.LimitTime.Format("2006-01-02 15:04:05")
	return output, nil
}

func (imp *DBOperateImp) QueryCdKeyGroup(ctx context.Context, input commonProtocol.CdKeyGroupQuery) (output commonProtocol.CdKeyGroup, err error) {
	// implement your code here:
	queryKey := "id = ?"
	queryValue := []string{fmt.Sprintf("%d", input.ID)}

	e, err := QueryModel[hyperNexusModel.CDKeyGroup](hyperNexusType.CDKeyGroup,
		"", queryKey, queryValue)
	if err != nil {
		return output, err
	}

	// Format response
	output.ID = e.ID
	output.CreateTime = e.CreatedAt.Format("2006-01-02 15:04:05")
	output.UpdateTime = e.UpdatedAt.Format("2006-01-02 15:04:05")
	output.Name = e.Name
	output.Desc = e.Desc
	output.Status = gconv.Int32(e.Status)
	return output, nil
}

func (imp *DBOperateImp) CreateCdKeyGroup(ctx context.Context, input commonProtocol.CdKeyGroup) (output commonProtocol.CdKeyGroup, err error) {
	// implement your code here:
	e, err := QueryModel[hyperNexusModel.CDKeyGroup](hyperNexusType.CDKeyGroup,
		"", "name = ?", []string{input.Name})
	if err == nil {
		return output, fmt.Errorf("cdkey group already exists")
	}

	e, err = CreateModel[hyperNexusModel.CDKeyGroup](hyperNexusType.CDKeyGroup, "")
	if err != nil {
		return output, err
	}

	e.Name = input.Name
	e.Desc = input.Desc
	e.Status = gconv.Uint8(input.Status)

	err = UpdateModel(hyperNexusType.CDKeyGroup, "", e)
	if err != nil {
		return output, err
	}

	// Format response
	output.ID = e.ID
	output.CreateTime = e.CreatedAt.Format("2006-01-02 15:04:05")
	output.UpdateTime = e.UpdatedAt.Format("2006-01-02 15:04:05")
	output.Name = e.Name
	output.Desc = e.Desc
	output.Status = gconv.Int32(e.Status)
	return output, nil
}

func (imp *DBOperateImp) UpdateCdKeyGroup(ctx context.Context, input commonProtocol.CdKeyGroup) (output commonProtocol.CdKeyGroup, err error) {
	// implement your code here:
	if input.ID == 0 {
		return output, fmt.Errorf("id is required")
	}

	e, err := QueryModel[hyperNexusModel.CDKeyGroup](hyperNexusType.CDKeyGroup,
		"", "id = ?", []string{fmt.Sprintf("%d", input.ID)})
	if err != nil {
		return output, err
	}

	e.Desc = input.Desc
	e.Name = input.Name
	e.Extra = input.Extra
	e.Status = gconv.Uint8(input.Status)

	err = UpdateModel(hyperNexusType.CDKeyGroup, "", e)
	if err != nil {
		return output, err
	}

	// Format response
	output.ID = e.ID
	output.CreateTime = e.CreatedAt.Format("2006-01-02 15:04:05")
	output.UpdateTime = e.UpdatedAt.Format("2006-01-02 15:04:05")
	output.Name = e.Name
	output.Desc = e.Desc
	output.Status = gconv.Int32(e.Status)
	return output, nil
}

func (imp *DBOperateImp) QueryCdKeyUse(ctx context.Context, input commonProtocol.CdKeyUseQuery) (output commonProtocol.CdKeyUseQueryResponse, err error) {
	// implement your code here:
	var where string
	var args []interface{}

	// 根据查询条件优先级：1. Codes不为空 2. HyperNexusAvatarId不为0 3. AvatarId
	if len(input.Codes) > 0 {
		// 如果Codes不为空，以Codes作为查询条件，使用QueryModelAll查询多条记录
		where = "codes IN (?)"
		args = append(args, input.Codes)

		// 查询数据库 - 使用QueryModelAll查询多条记录
		results, err := QueryModelAll[hyperNexusModel.CDKeyUse](hyperNexusType.CDKeyUse, input.AccountName, where, args...)
		if err != nil {
			return output, err
		}

		// 转换查询结果为响应格式
		output.UseData = make([]*commonProtocol.CdKeyUse, 0, len(results))
		for _, e := range results {
			cdKeyUse := &commonProtocol.CdKeyUse{
				ID:                 e.ID,
				CreateTime:         e.CreatedAt.Format("2006-01-02 15:04:05"),
				UpdateTime:         e.UpdatedAt.Format("2006-01-02 15:04:05"),
				AvatarId:           e.AvatarId,
				HyperNexusAvatarId: e.HyperNexusAvatarId,
				Codes:              e.Codes,
			}
			output.UseData = append(output.UseData, cdKeyUse)
		}
	} else {
		// 对于HyperNexusAvatarId或AvatarId查询，使用QueryModel查询单条记录
		if input.HyperNexusAvatarId != 0 {
			// 如果HyperNexusAvatarId不为0，以HyperNexusAvatarId作为查询条件
			where = "hyperNexusAvatarId = ?"
			args = append(args, input.HyperNexusAvatarId)
		} else {
			// 否则以AvatarId作为查询条件
			where = "avatarId = ?"
			args = append(args, input.AvatarId)
		}

		// 查询数据库 - 使用QueryModel查询单条记录
		e, err := QueryModel[hyperNexusModel.CDKeyUse](hyperNexusType.CDKeyUse, input.AccountName, where, args...)
		if err != nil {
			return output, err
		}

		// 转换查询结果为响应格式
		output.UseData = make([]*commonProtocol.CdKeyUse, 0, 1)
		cdKeyUse := &commonProtocol.CdKeyUse{
			ID:                 e.ID,
			CreateTime:         e.CreatedAt.Format("2006-01-02 15:04:05"),
			UpdateTime:         e.UpdatedAt.Format("2006-01-02 15:04:05"),
			AvatarId:           e.AvatarId,
			HyperNexusAvatarId: e.HyperNexusAvatarId,
			Codes:              e.Codes,
		}
		output.UseData = append(output.UseData, cdKeyUse)
	}

	return output, nil
}

func (imp *DBOperateImp) CreateCdKeyUse(ctx context.Context, input commonProtocol.CdKeyUseCreateOrUpdate) (output commonProtocol.CdKeyUse, err error) {
	// implement your code here:
	// 检查必要参数
	if input.CdKeyUse.AvatarId == 0 && input.CdKeyUse.HyperNexusAvatarId == 0 {
		return output, fmt.Errorf("avatarId or hyperNexusAvatarId is required")
	}

	if len(input.CdKeyUse.Codes) == 0 {
		return output, fmt.Errorf("codes is required")
	}

	// 创建CDKeyUse记录
	e, err := CreateModel[hyperNexusModel.CDKeyUse](hyperNexusType.CDKeyUse, input.AccountName)
	if err != nil {
		return output, err
	}

	// 设置字段值
	e.AvatarId = input.CdKeyUse.AvatarId
	e.HyperNexusAvatarId = input.CdKeyUse.HyperNexusAvatarId
	e.Codes = input.CdKeyUse.Codes

	// 更新到数据库
	err = UpdateModel(hyperNexusType.CDKeyUse, input.AccountName, e)
	if err != nil {
		return output, err
	}

	// 格式化返回结果
	output.ID = e.ID
	output.CreateTime = e.CreatedAt.Format("2006-01-02 15:04:05")
	output.UpdateTime = e.UpdatedAt.Format("2006-01-02 15:04:05")
	output.AvatarId = e.AvatarId
	output.HyperNexusAvatarId = e.HyperNexusAvatarId
	output.Codes = e.Codes
	return output, nil
}

func (imp *DBOperateImp) UpdateCdKeyUse(ctx context.Context, input commonProtocol.CdKeyUseCreateOrUpdate) (output commonProtocol.CdKeyUse, err error) {
	// implement your code here:
	if input.CdKeyUse.ID == 0 {
		return output, fmt.Errorf("id is required")
	}

	e, err := QueryModel[hyperNexusModel.CDKeyUse](hyperNexusType.CDKeyUse,
		input.AccountName, "id = ?", []string{fmt.Sprintf("%d", input.CdKeyUse.ID)})
	if err != nil {
		return output, err
	}
	e.Codes = input.CdKeyUse.Codes
	err = UpdateModel(hyperNexusType.CDKeyUse, input.AccountName, e)
	if err != nil {
		return output, err
	}

	// Format response
	output.ID = e.ID
	output.CreateTime = e.CreatedAt.Format("2006-01-02 15:04:05")
	output.UpdateTime = e.UpdatedAt.Format("2006-01-02 15:04:05")
	output.AvatarId = e.AvatarId
	output.HyperNexusAvatarId = e.HyperNexusAvatarId
	output.Codes = e.Codes
	return output, nil
}

func (imp *DBOperateImp) QueryCenter(ctx context.Context, input commonProtocol.CenterQuery) (output commonProtocol.Center, err error) {
	// implement your code here:
	queryKey := "account = ?"
	queryValue := []string{input.Account}

	e, err := QueryModel[hyperNexusModel.Center](hyperNexusType.Center,
		input.Account, queryKey, queryValue)
	if err != nil {
		return output, err
	}

	// Format response
	output.ID = e.ID
	output.CreateTime = e.CreatedAt.Format("2006-01-02 15:04:05")
	output.UpdateTime = e.UpdatedAt.Format("2006-01-02 15:04:05")
	output.Account = e.Account
	output.Data = gconv.Int32s(e.Data)
	output.LastTime = e.LastTime.Format("2006-01-02 15:04:05")
	return output, nil
}

func (imp *DBOperateImp) CreateCenter(ctx context.Context, input commonProtocol.Center) (output commonProtocol.Center, err error) {
	// implement your code here:
	e, err := QueryModel[hyperNexusModel.Center](hyperNexusType.Center,
		input.Account, "account = ?", []string{input.Account})
	if err == nil {
		return output, fmt.Errorf("center already exists")
	}

	e, err = CreateModel[hyperNexusModel.Center](hyperNexusType.Center, input.Account)
	if err != nil {
		return output, err
	}

	e.Account = input.Account
	e.Data = gconv.Uint32s(input.Data)
	e.LastTime = gconv.Time(input.LastTime)

	err = UpdateModel(hyperNexusType.Center, input.Account, e)
	if err != nil {
		return output, err
	}

	// Format response
	output.ID = e.ID
	output.CreateTime = e.CreatedAt.Format("2006-01-02 15:04:05")
	output.UpdateTime = e.UpdatedAt.Format("2006-01-02 15:04:05")
	output.Account = e.Account
	output.Data = gconv.Int32s(e.Data)
	output.LastTime = e.LastTime.Format("2006-01-02 15:04:05")
	return output, nil
}

func (imp *DBOperateImp) UpdateCenter(ctx context.Context, input commonProtocol.Center) (output commonProtocol.Center, err error) {
	// implement your code here:
	if input.ID == 0 {
		return output, fmt.Errorf("id is required")
	}

	e, err := QueryModel[hyperNexusModel.Center](hyperNexusType.Center,
		input.Account, "account = ?", []string{input.Account})
	if err != nil {
		return output, err
	}

	e.Data = gconv.Uint32s(input.Data)
	e.LastTime = gconv.Time(input.LastTime)

	err = UpdateModel(hyperNexusType.Center, input.Account, e)
	if err != nil {
		return output, err
	}

	// Format response
	output.ID = e.ID
	output.CreateTime = e.CreatedAt.Format("2006-01-02 15:04:05")
	output.UpdateTime = e.UpdatedAt.Format("2006-01-02 15:04:05")
	output.Account = e.Account
	output.Data = gconv.Int32s(e.Data)
	output.LastTime = e.LastTime.Format("2006-01-02 15:04:05")
	return output, nil
}

func (imp *DBOperateImp) QueryGameConfig(ctx context.Context, input emptypb.Empty) (output commonProtocol.GameConfigQueryResponse, err error) {
	// implement your code here:
	// 查询所有游戏配置信息
	results, err := QueryModelAll[hyperNexusModel.GameConfig](hyperNexusType.GameConfig, "", "")
	if err != nil {
		return output, err
	}

	// 初始化响应
	output.Configs = make([]*commonProtocol.GameConfig, 0, len(results))

	// 格式化每个配置项
	for _, e := range results {
		config := &commonProtocol.GameConfig{
			ID:         int32(e.ID),
			CreateTime: e.CreatedAt.Format("2006-01-02 15:04:05"),
			UpdateTime: e.UpdatedAt.Format("2006-01-02 15:04:05"),
			Name:       e.Name,
			Type:       int32(e.Type),
			ParentId:   int32(e.ParentId),
			Value:      e.Value,
			IsActive:   int32(e.IsActive),
			Platform:   int32(e.Platform),
		}
		output.Configs = append(output.Configs, config)
	}

	return output, nil
}

func (imp *DBOperateImp) CreateGameConfig(ctx context.Context, input commonProtocol.GameConfig) (output commonProtocol.GameConfig, err error) {
	// implement your code here:

	e, err := CreateModel[hyperNexusModel.GameConfig](hyperNexusType.GameConfig, "")
	if err != nil {
		return output, err
	}

	e.Name = input.Name
	e.Type = gconv.Int8(input.Type)
	e.ParentId = gconv.Uint32(input.ParentId)
	e.Value = input.Value
	e.IsActive = gconv.Int8(input.IsActive)
	e.Platform = gconv.Int8(input.Platform)
	err = UpdateModel(hyperNexusType.GameConfig, "", e)
	if err != nil {
		return output, err
	}
	// Format response
	output.ID = e.ID
	output.CreateTime = e.CreatedAt.Format("2006-01-02 15:04:05")
	output.UpdateTime = e.UpdatedAt.Format("2006-01-02 15:04:05")
	output.Name = e.Name
	output.ParentId = gconv.Int32(e.ParentId)
	output.Type = gconv.Int32(e.Type)
	output.Value = e.Value
	output.IsActive = gconv.Int32(e.IsActive)
	return output, nil
}

func (imp *DBOperateImp) UpdateGameConfig(ctx context.Context, input commonProtocol.GameConfig) (output commonProtocol.GameConfig, err error) {
	// implement your code here:
	if input.ID == 0 {
		return output, fmt.Errorf("id is required")
	}

	e, err := QueryModel[hyperNexusModel.GameConfig](hyperNexusType.GameConfig,
		"", "id = ?", []string{fmt.Sprintf("%d", input.ID)})
	if err != nil {
		return output, err
	}

	e.Name = input.Name
	e.Type = gconv.Int8(input.Type)
	e.ParentId = gconv.Uint32(input.ParentId)
	e.Value = input.Value
	e.IsActive = gconv.Int8(input.IsActive)
	e.Platform = gconv.Int8(input.Platform)
	err = UpdateModel(hyperNexusType.GameConfig, "", e)
	if err != nil {
		return output, err
	}
	// Format response
	output.ID = e.ID
	output.CreateTime = e.CreatedAt.Format("2006-01-02 15:04:05")
	output.UpdateTime = e.UpdatedAt.Format("2006-01-02 15:04:05")
	output.Name = e.Name
	output.ParentId = gconv.Int32(e.ParentId)
	output.Type = gconv.Int32(e.Type)
	output.Value = e.Value
	output.IsActive = gconv.Int32(e.IsActive)
	return output, nil
}

func (imp *DBOperateImp) QueryOrder(ctx context.Context, input commonProtocol.OrderQuery) (output commonProtocol.OrderQueryResponse, err error) {
	// implement your code here:
	// 初始化响应
	output.Orders = make([]*commonProtocol.Order, 0)

	if input.HyperNexusAvatarId > 0 {
		// 当HyperNexusAvatarId大于0时，获取所有等于HyperNexusAvatarId的订单信息
		whereCmd := "hyperNexusAvatarId =?"
		var args []interface{}
		args = append(args, input.HyperNexusAvatarId)
		if input.Order != "" {
			whereCmd += " and orderId =?"
			args = append(args, input.Order)
		}
		orders, err := QueryModelAll[hyperNexusModel.Order](hyperNexusType.Order,
			input.AccountName, whereCmd, args...)
		if err != nil {
			return output, err
		}

		if len(orders) == 0 {
			return output, nil
		}

		// 将查询结果转换为响应格式
		for _, order := range orders {
			orderInfo := &commonProtocol.Order{
				ID:                 int32(order.ID),
				CreateTime:         order.CreatedAt.Format("2006-01-02 15:04:05"),
				UpdateTime:         order.UpdatedAt.Format("2006-01-02 15:04:05"),
				HyperNexusAvatarId: order.HyperNexusAvatarId,
				Account:            order.Account,
				AvatarId:           order.AvatarId,
				Order:              order.Order,
				Data:               order.Data,
				Status:             int32(order.Status),
				Goods:              order.Goods,
				GoodsContent:       order.GoodsContent,
				GoodsPrice:         order.GoodsPrice,
			}
			output.Orders = append(output.Orders, orderInfo)
		}
	} else {
		// 当HyperNexusAvatarId等于0时，获取查询参数Order指定的订单信息
		if input.Order == "" {
			return output, fmt.Errorf("订单号不能为空")
		}

		order, err := QueryModel[hyperNexusModel.Order](hyperNexusType.Order,
			input.AccountName, "orderId = ?", []string{input.Order})
		if err != nil {
			return output, err
		}

		// 将查询结果转换为响应格式
		orderInfo := &commonProtocol.Order{
			ID:                 int32(order.ID),
			CreateTime:         order.CreatedAt.Format("2006-01-02 15:04:05"),
			UpdateTime:         order.UpdatedAt.Format("2006-01-02 15:04:05"),
			HyperNexusAvatarId: order.HyperNexusAvatarId,
			Account:            order.Account,
			AvatarId:           order.AvatarId,
			Order:              order.Order,
			Data:               order.Data,
			Status:             int32(order.Status),
			Goods:              order.Goods,
			GoodsContent:       order.GoodsContent,
			GoodsPrice:         order.GoodsPrice,
		}
		output.Orders = append(output.Orders, orderInfo)
	}

	return output, nil
}

func (imp *DBOperateImp) CreateOrder(ctx context.Context, input commonProtocol.Order) (output commonProtocol.Order, err error) {
	// implement your code here:
	e, err := QueryModel[hyperNexusModel.Order](hyperNexusType.Order,
		input.Account, "orderId = ?", []string{input.Order})
	if err == nil {
		return output, fmt.Errorf("order already exists")
	}

	e, err = CreateModel[hyperNexusModel.Order](hyperNexusType.Order, input.Account)
	if err != nil {
		return output, err
	}
	e.Account = input.Account
	e.AvatarId = input.AvatarId
	e.HyperNexusAvatarId = input.HyperNexusAvatarId
	e.Order = input.Order
	e.Data = input.Data
	e.Status = input.Status
	e.Goods = input.Goods
	e.GoodsContent = input.GoodsContent
	e.GoodsPrice = input.GoodsPrice
	err = UpdateModel(hyperNexusType.Order, input.Account, e)
	if err != nil {
		return output, err
	}

	// Format response
	output.ID = e.ID
	output.CreateTime = e.CreatedAt.Format("2006-01-02 15:04:05")
	output.UpdateTime = e.UpdatedAt.Format("2006-01-02 15:04:05")
	output.Account = e.Account
	output.AvatarId = e.AvatarId
	output.HyperNexusAvatarId = e.HyperNexusAvatarId
	output.Order = e.Order
	output.Data = e.Data
	output.Status = e.Status
	output.Goods = e.Goods
	output.GoodsContent = e.GoodsContent
	output.GoodsPrice = e.GoodsPrice
	return output, nil
}

func (imp *DBOperateImp) UpdateOrder(ctx context.Context, input commonProtocol.Order) (output commonProtocol.Order, err error) {
	// implement your code here:
	if input.ID == 0 {
		return output, fmt.Errorf("id is required")
	}

	e, err := QueryModel[hyperNexusModel.Order](hyperNexusType.Order,
		input.Account, "id = ?", []string{fmt.Sprintf("%d", input.ID)})
	if err != nil {
		return output, err
	}

	e.Status = input.Status

	err = UpdateModel(hyperNexusType.Order, input.Account, e)
	if err != nil {
		return output, err
	}

	// Format response
	output.ID = e.ID
	output.CreateTime = e.CreatedAt.Format("2006-01-02 15:04:05")
	output.UpdateTime = e.UpdatedAt.Format("2006-01-02 15:04:05")
	output.Account = e.Account
	output.AvatarId = e.AvatarId
	output.HyperNexusAvatarId = e.HyperNexusAvatarId
	output.Order = e.Order
	output.Data = e.Data
	output.Status = e.Status
	output.Goods = e.Goods
	output.GoodsContent = e.GoodsContent
	output.GoodsPrice = e.GoodsPrice
	return output, nil
}

func (imp *DBOperateImp) QueryServer(ctx context.Context, input commonProtocol.ServerQuery) (output commonProtocol.ServerQueryResponse, err error) {
	// 初始化响应
	output.Servers = make([]*commonProtocol.Server, 0)

	if input.Zone == 0 {
		// 当ID为0时，查询所有服务器信息
		servers, err := QueryModelAll[hyperNexusModel.Server](hyperNexusType.Server, "", "")
		if err != nil {
			return output, err
		}

		// 将查询结果转换为响应格式
		for _, server := range servers {
			serverInfo := &commonProtocol.Server{
				ID:             server.ID,
				CreateTime:     server.CreatedAt.Format("2006-01-02 15:04:05"),
				UpdateTime:     server.UpdatedAt.Format("2006-01-02 15:04:05"),
				Name:           server.Name,
				Zone:           server.Zone,
				ThirdZone:      server.ThirdZone,
				Address:        server.Address,
				Port:           server.Port,
				WebAddress:     server.WebAddress,
				WebPort:        server.WebPort,
				WebSsl:         server.WebSsl,
				WebSecret:      server.WebSecret,
				WebAddressBack: server.WebAddressBack,
				Status:         server.Status,
				Sort:           server.Sort,
				Description:    server.Description,
				OpenTime:       server.OpenTime.Format("2006-01-02 15:04:05"),
			}
			output.Servers = append(output.Servers, serverInfo)
		}
	} else {
		// 当ID不为0时，查询指定服务器信息
		server, err := QueryModel[hyperNexusModel.Server](hyperNexusType.Server,
			"", "zone = ?", []string{fmt.Sprintf("%d", input.Zone)})
		if err != nil {
			return output, err
		}

		// 将查询结果转换为响应格式
		serverInfo := &commonProtocol.Server{
			ID:             server.ID,
			CreateTime:     server.CreatedAt.Format("2006-01-02 15:04:05"),
			UpdateTime:     server.UpdatedAt.Format("2006-01-02 15:04:05"),
			Name:           server.Name,
			Zone:           server.Zone,
			ThirdZone:      server.ThirdZone,
			Address:        server.Address,
			Port:           server.Port,
			WebAddress:     server.WebAddress,
			WebPort:        server.WebPort,
			WebSsl:         server.WebSsl,
			WebSecret:      server.WebSecret,
			WebAddressBack: server.WebAddressBack,
			Status:         server.Status,
			Sort:           server.Sort,
			Description:    server.Description,
			OpenTime:       server.OpenTime.Format("2006-01-02 15:04:05"),
		}
		output.Servers = append(output.Servers, serverInfo)
	}

	return output, nil
}

func (imp *DBOperateImp) CreateServer(ctx context.Context, input commonProtocol.Server) (output commonProtocol.Server, err error) {
	// implement your code here:
	e, err := QueryModel[hyperNexusModel.Server](hyperNexusType.Server,
		"", "zone = ?", []string{gconv.String(input.Zone)})
	if err == nil {
		return output, fmt.Errorf("server already exists")
	}

	e, err = CreateModel[hyperNexusModel.Server](hyperNexusType.Server, "")
	if err != nil {
		return output, err
	}

	e.Name = input.Name
	e.Zone = input.Zone
	e.ThirdZone = input.ThirdZone
	e.Address = input.Address
	e.Port = input.Port
	e.WebAddress = input.WebAddress
	e.WebPort = input.WebPort
	e.WebSsl = input.WebSsl
	e.WebSecret = input.WebSecret
	e.WebAddressBack = input.WebAddressBack
	e.Status = input.Status
	e.Sort = input.Sort
	e.Description = input.Description
	e.OpenTime = time.Now()
	err = UpdateModel(hyperNexusType.Server, "", e)

	if err != nil {
		return output, err
	}

	// Format response
	output.ID = e.ID
	output.CreateTime = e.CreatedAt.Format("2006-01-02 15:04:05")
	output.UpdateTime = e.UpdatedAt.Format("2006-01-02 15:04:05")
	output.Name = e.Name
	output.Zone = e.Zone
	output.ThirdZone = e.ThirdZone
	output.Address = e.Address
	output.Port = e.Port
	output.WebAddress = e.WebAddress
	output.WebPort = e.WebPort
	output.WebSsl = e.WebSsl
	output.WebSecret = e.WebSecret
	output.WebAddressBack = e.WebAddressBack
	output.Status = e.Status
	output.Sort = e.Sort
	output.Description = e.Description
	output.OpenTime = e.OpenTime.Format("2006-01-02 15:04:05")
	return output, nil
}

func (imp *DBOperateImp) UpdateServer(ctx context.Context, input commonProtocol.Server) (output commonProtocol.Server, err error) {
	// implement your code here:
	if input.ID == 0 {
		return output, fmt.Errorf("id is required")
	}

	e, err := QueryModel[hyperNexusModel.Server](hyperNexusType.Server,
		"", "id = ?", []string{fmt.Sprintf("%d", input.ID)})
	if err != nil {
		return output, err
	}
	e.Name = input.Name
	e.Zone = input.Zone
	e.ThirdZone = input.ThirdZone
	e.Address = input.Address
	e.Port = input.Port
	e.WebAddress = input.WebAddress
	e.WebPort = input.WebPort
	e.WebSsl = input.WebSsl
	e.WebSecret = input.WebSecret
	e.WebAddressBack = input.WebAddressBack
	e.Status = input.Status
	e.Sort = input.Sort
	e.Description = input.Description
	e.OpenTime = gconv.Time(input.OpenTime)
	err = UpdateModel(hyperNexusType.Server, "", e)
	if err != nil {
		return output, err
	}
	// Format response
	output.ID = e.ID
	output.CreateTime = e.CreatedAt.Format("2006-01-02 15:04:05")
	output.UpdateTime = e.UpdatedAt.Format("2006-01-02 15:04:05")
	output.Name = e.Name
	output.Zone = e.Zone
	output.ThirdZone = e.ThirdZone
	output.Address = e.Address
	output.Port = e.Port
	output.WebAddress = e.WebAddress
	output.WebPort = e.WebPort
	output.WebSsl = e.WebSsl
	output.WebSecret = e.WebSecret
	output.WebAddressBack = e.WebAddressBack
	output.Status = e.Status
	output.Sort = e.Sort
	output.Description = e.Description
	output.OpenTime = e.OpenTime.Format("2006-01-02 15:04:05")
	return output, nil
}

func (imp *DBOperateImp) QuerySubscribe(ctx context.Context, input commonProtocol.SubscribeQuery) (output commonProtocol.SubscribeQueryResponse, err error) {
	// implement your code here:

	// 初始化响应
	output.Subscribes = make([]*commonProtocol.Subscribe, 0)

	// 构建查询条件
	var conditions []string
	var values []interface{}
	var hasCondition bool

	// 组合多个查询条件
	if input.AccountName != "" {
		conditions = append(conditions, "accountName = ?")
		values = append(values, input.AccountName)
		hasCondition = true
	}

	if input.HyperNexusAvatarId > 0 {
		conditions = append(conditions, "hyperNexusAvatarId = ?")
		values = append(values, fmt.Sprintf("%d", input.HyperNexusAvatarId))
		hasCondition = true
	}

	if input.AvatarId > 0 {
		conditions = append(conditions, "avatarId = ?")
		values = append(values, fmt.Sprintf("%d", input.AvatarId))
		hasCondition = true
	}

	if input.ThirdPlatformType > 0 {
		conditions = append(conditions, "thirdPlatformType = ?")
		values = append(values, fmt.Sprintf("%d", input.ThirdPlatformType))
		hasCondition = true
	}

	// 检查是否有有效的查询条件
	if !hasCondition {
		return output, fmt.Errorf("至少需要提供一个查询条件")
	}

	// 将条件组合成SQL查询语句
	queryKey := strings.Join(conditions, " AND ")

	// 执行查询
	subscribes, err := QueryModelAll[hyperNexusModel.Subscribe](hyperNexusType.Subscribe,
		input.AccountName, queryKey, values...)
	if err != nil {
		return output, err
	}

	if len(subscribes) == 0 {
		return output, nil
	}

	// 将查询结果转换为响应格式
	for _, subscribe := range subscribes {
		subscribeInfo := &commonProtocol.Subscribe{
			ID:                 subscribe.ID,
			CreateTime:         subscribe.CreatedAt.Format("2006-01-02 15:04:05"),
			UpdateTime:         subscribe.UpdatedAt.Format("2006-01-02 15:04:05"),
			AccountName:        subscribe.AccountName,
			SubscribeType:      subscribe.SubscribeType,
			AvatarId:           subscribe.AvatarId,
			HyperNexusAvatarId: subscribe.HyperNexusAvatarId,
			ThirdPlatformType:  subscribe.ThirdPlatformType,
			SendTime:           subscribe.SendTime.Format("2006-01-02 15:04:05"),
			LastSendTime:       subscribe.LastSendTime.Format("2006-01-02 15:04:05"),
			ConditionType:      subscribe.ConditionType,
			ConditionValue:     subscribe.ConditionValue,
			Result:             subscribe.Result,
			Status:             subscribe.Status,
		}
		output.Subscribes = append(output.Subscribes, subscribeInfo)
	}
	return output, nil
}

func (imp *DBOperateImp) CreateSubscribe(ctx context.Context, input commonProtocol.Subscribe) (output commonProtocol.Subscribe, err error) {
	// implement your code here:
	e, err := CreateModel[hyperNexusModel.Subscribe](hyperNexusType.Subscribe, input.AccountName)
	if err != nil {
		return output, err
	}

	e.AccountName = input.AccountName
	e.AvatarId = input.AvatarId
	e.HyperNexusAvatarId = input.HyperNexusAvatarId
	e.ThirdPlatformType = input.ThirdPlatformType
	e.SubscribeType = input.SubscribeType
	e.SendTime = gconv.Time(input.SendTime)
	e.LastSendTime = gconv.Time(input.LastSendTime)
	e.ConditionType = input.ConditionType
	e.ConditionValue = input.ConditionValue
	e.Result = input.Result
	e.Status = input.Status

	err = UpdateModel(hyperNexusType.Subscribe, input.AccountName, e)
	if err != nil {
		return output, err
	}

	// Format response
	output.ID = e.ID
	output.CreateTime = e.CreatedAt.Format("2006-01-02 15:04:05")
	output.UpdateTime = e.UpdatedAt.Format("2006-01-02 15:04:05")
	output.AccountName = e.AccountName
	output.SubscribeType = e.SubscribeType
	output.AvatarId = e.AvatarId
	output.HyperNexusAvatarId = e.HyperNexusAvatarId
	output.ThirdPlatformType = e.ThirdPlatformType
	output.SendTime = e.SendTime.Format("2006-01-02 15:04:05")
	output.LastSendTime = e.LastSendTime.Format("2006-01-02 15:04:05")
	output.ConditionType = e.ConditionType
	output.ConditionValue = e.ConditionValue
	output.Result = e.Result
	output.Status = e.Status
	return output, nil
}

func (imp *DBOperateImp) UpdateSubscribe(ctx context.Context, input commonProtocol.Subscribe) (output commonProtocol.Subscribe, err error) {
	// implement your code here:
	if input.ID == 0 {
		return output, fmt.Errorf("id is required")
	}

	e, err := QueryModel[hyperNexusModel.Subscribe](hyperNexusType.Subscribe,
		input.AccountName, "id = ?", []string{fmt.Sprintf("%d", input.ID)})
	if err != nil {
		return output, err
	}

	e.SubscribeType = input.SubscribeType
	e.AccountName = input.AccountName
	e.AvatarId = input.AvatarId
	e.HyperNexusAvatarId = input.HyperNexusAvatarId
	e.ThirdPlatformType = input.ThirdPlatformType
	e.SendTime = gconv.Time(input.SendTime)
	e.LastSendTime = gconv.Time(input.LastSendTime)
	e.ConditionType = input.ConditionType
	e.ConditionValue = input.ConditionValue
	e.Result = input.Result
	e.Status = input.Status

	err = UpdateModel(hyperNexusType.Subscribe, input.AccountName, e)
	if err != nil {
		return output, err
	}

	// Format response
	output.ID = e.ID
	output.CreateTime = e.CreatedAt.Format("2006-01-02 15:04:05")
	output.UpdateTime = e.UpdatedAt.Format("2006-01-02 15:04:05")
	output.AccountName = e.AccountName
	output.SubscribeType = e.SubscribeType
	output.AvatarId = e.AvatarId
	output.HyperNexusAvatarId = e.HyperNexusAvatarId
	output.ThirdPlatformType = e.ThirdPlatformType
	output.SendTime = e.SendTime.Format("2006-01-02 15:04:05")
	output.LastSendTime = e.LastSendTime.Format("2006-01-02 15:04:05")
	output.ConditionType = e.ConditionType
	output.ConditionValue = e.ConditionValue
	output.Result = e.Result
	output.Status = e.Status
	return output, nil
}

func (imp *DBOperateImp) QueryPlatform(ctx context.Context, input commonProtocol.PlatformQuery) (output commonProtocol.PlatformQueryResponse, err error) {
	// implement your code here:
	for _, key := range input.PlatformKeys {
		model, err := QueryModel[hyperNexusModel.Platform](hyperNexusType.Platform,
			"", "platformKey = ?", []string{key})
		if err != nil {
			continue
		}
		output.Platforms = append(output.Platforms, &commonProtocol.Platform{
			PlatformKey:   model.PlatformKey,
			PlatformValue: model.PlatformValue,
		})
	}

	return output, nil
}

func (imp *DBOperateImp) CreatePlatform(ctx context.Context, input commonProtocol.Platform) (output commonProtocol.Platform, err error) {
	// implement your code here:
	_, err = QueryModel[hyperNexusModel.Platform](hyperNexusType.Platform,
		"", "platformKey = ?", []string{input.PlatformKey})
	if err == nil {
		return output, fmt.Errorf("platform already exists")
	}

	e, err := CreateModel[hyperNexusModel.Platform](hyperNexusType.Platform, "")
	if err != nil {
		return output, err
	}

	e.PlatformKey = input.PlatformKey
	e.PlatformValue = input.PlatformValue

	err = UpdateModel(hyperNexusType.Platform, "", e)
	if err != nil {
		return output, err
	}

	output.PlatformKey = e.PlatformKey
	output.PlatformValue = e.PlatformValue
	return output, nil
}

func (imp *DBOperateImp) UpdatePlatform(ctx context.Context, input commonProtocol.Platform) (output commonProtocol.Platform, err error) {
	// implement your code here:
	if input.PlatformKey == "" {
		return output, fmt.Errorf("platformKey is required")
	}

	e, err := QueryModel[hyperNexusModel.Platform](hyperNexusType.Platform,
		"", "platformKey = ?", []string{input.PlatformKey})
	if err != nil {
		return output, err
	}

	e.PlatformValue = input.PlatformValue

	err = UpdateModel(hyperNexusType.Platform, "", e)
	if err != nil {
		return output, err
	}

	output.PlatformKey = e.PlatformKey
	output.PlatformValue = e.PlatformValue
	return output, nil
}
